home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL04.TXT < prev    next >
Text File  |  1986-04-05  |  6KB  |  205 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 16
  2.  
  3.  
  4. TURBO-LESSON 4:  DECLARATIONS, INPUT
  5.  
  6. OBJECTIVES - In Lesson 4 you will learn about:
  7.  
  8. 1.  The DECLARATIONS part of a program
  9. 2.  VAR declaration
  10. 3.  Input using the ReadLn statement
  11. 4.  Integer variables
  12.  
  13.  
  14. 1.  The DECLARATIONS part of a program.
  15.  
  16. You learned in the previous lesson that there are two main parts 
  17. to any PASCAL program: DECLARATIONS, and MAIN BODY.   The various 
  18. entries in the DECLARATIONS section define the data items used in 
  19. the processing in the MAIN BODY.  Not all declaration entries 
  20. will occur in every program, but only the ones needed to support 
  21. the processing.  
  22.  
  23. The various types of declaration entries will be introduced as 
  24. needed in the sample programs.  Only the VAR entry will be used 
  25. in this program.  
  26.  
  27.  
  28. 2.  VAR declaration.
  29.  
  30. All variables, (spelled A-L-L, no exceptions), must be defined 
  31. before they are referenced by processing statements.  The VAR 
  32. entry is used to define variables.  The form of the entry is:
  33.  
  34. variable-name   :  type;
  35.  
  36. The variable-name may be one or several variable-names separated 
  37. by commas.  The type may be a predefined type, such as Integer, 
  38. or a type you have constructed useing the predefined types. The 
  39. colon must occur between the variable-name(s) and the type.  
  40. Extra spaces are acceptable to allow more readable format.  Below 
  41. are some VAR entries: 
  42.  
  43. VAR
  44.       i,j,k    : Integer;
  45.       Inkey    : Char;
  46.       Rate     : Real;
  47.       Count    : Integer;
  48.  
  49. (The example above includes types not yet discussed, to 
  50. illustrate the form of the VAR entry.)
  51. î
  52. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 17
  53.  
  54.  
  55. ##### DO:
  56.  
  57. Look at PROG4.  
  58.  
  59. (You know from earlier lessons how to load the program and use 
  60. the editor to look at the program.)  A segment of PROG4 is: 
  61.  
  62. VAR
  63.   Number   : Integer;
  64.  
  65.  
  66. The VAR entry above defines a variable called "Number" to be of 
  67. type "Integer".  This means the computer must set up a memory 
  68. location large enough to store an integer, which can be accessed 
  69. by referring to the name "Number".  
  70.  
  71. Notice the variable, Number, is later referenced in the 
  72. processing statements.
  73.  
  74. ##### DO:
  75.  
  76. Add an integer variable called "Age" to Prog4.  This will be used 
  77. later in this lesson.  You can either add the new variable to the 
  78. declaration of Number
  79.  
  80.       Number, Age   : Integer;
  81.  
  82. or add another declaration
  83.  
  84.       Number   : Integer;
  85.       Age      : Integer;
  86.  
  87. Compile the program to be sure you haven't made a syntax error.     
  88.  
  89.  
  90. 3.  Input using the ReadLn statement.
  91.  
  92. ReadLn is the statement used to input variables.  The form of the 
  93. statement is:
  94.  
  95. ReadLn(var_1, var_2, . . . ,var_n);
  96.  
  97. When the statement is executed, the computer will wait for you to 
  98. type values for the variables, separated by one or more spaces, 
  99. followed by depressing the enter key.  
  100. î
  101. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 18
  102.  
  103.  
  104. Part of PROG4 follows:
  105.                                                              
  106. BEGIN
  107.   Write('Enter a number (no decimals please): ');
  108.   ReadLn(Number);
  109.   WriteLn;                        { Display one blank line }
  110.   WriteLn('Number: ',Number);     { Display the number entered }
  111. END.
  112.  
  113. The Write statement provides a prompt before the ReadLn accepts 
  114. Number.  Notice that ReadLn does not provide a "?" or any other 
  115. prompt.  The programmer must provide any prompting require.  
  116.  
  117. The use of the Write statement for prompting, instead of the 
  118. WriteLn, keeps the cursor on the same line so the input will 
  119. occur right after the prompt message.
  120.  
  121. The ReadLn accepts the number you type and stores it in the 
  122. memory location which the computer has set aside for the Integer 
  123. variable, Number.  
  124.  
  125. ##### DO:
  126.  
  127. Compile and run PROG4.  When prompted, type 12 and enter.  Run it 
  128. again and enter -34.  
  129.  
  130. ##### DO:
  131.  
  132. Using the statements of PROG4 as an example, expand the program 
  133. to do the following:
  134.  
  135. (1)   Write a prompt to 'Enter your age'.
  136. (2)   Read a value for age to be stored in the integer variable, 
  137. Age, which you added to the VAR declarations in the previous 
  138. section.
  139. (3)   Write a message which prints the age entered, in a manner 
  140. similar to the way Number was printed.
  141.  
  142. Run the program.
  143.  
  144.  
  145. 4.  Integer Variables. 
  146.  
  147. Integers are counting numbers, with no decimal points.  They 
  148. may be positive or negative.  In TURBO the range of Integers 
  149. permitted is:
  150.  
  151.        -32768 to +32767
  152.  
  153. Integers are used for subscripts, indexes, counting, input and 
  154. output of such things as counts, limits, menu choices.  Decimal 
  155. numbers (Real type, discussed later), are needed for such things 
  156. as dollar amounts and calculations.   When decimal numbers are 
  157. not actually needed, Integers should be used since they are 
  158. easier to use and take less memory.  
  159. î
  160. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 19
  161.  
  162.  
  163. ##### DO:
  164.  
  165. Run PROG4 again, this time entering data which will cause errors.
  166.  
  167. When prompted to enter a number, enter the letter A instead.  
  168. What happened?
  169.  
  170. A message appears:
  171.  
  172. I/O error 10, PC=287C   
  173. Program aborted
  174.  
  175. Searching 
  176.   15 lines
  177.  
  178. Run-time error position found. Press <ESC>
  179.  
  180. What this means is as follows:
  181.  
  182. The program aborted because "I/O error 10" occurred at program 
  183. code address, 287C.  (Your program may produce a different 
  184. program code address.)  To find out what "I/O error 10" is, look 
  185. in your reference manual in the appendix containing "I/O ERROR 
  186. MESSAGES".  Error 10 is an "Error in numeric format".
  187.  
  188. TURBO searched 15 lines before finding where the error occurred.
  189.  
  190. When you press the ESC key, the editor is activated with the 
  191. cursor positioned at the end of the statement which caused the 
  192. error.  (In this case, it is not the statement which caused the 
  193. problem, but the type of data entered.)
  194.  
  195. ##### DO:
  196.  
  197. Run the program again, entering too large a number, 88888.  Note 
  198. that the same error occurs.  Run it again with too small a 
  199. number, -55555.  You may want to experiment with the end-points 
  200. of the acceptable integer range: -32768 to 32767.  For example, 
  201. try entering the values -32767, -32768, -32769.
  202.  
  203. NOTE: THERE ARE WAYS TO HANDLE ERRORS TO AVOID ABORTING THE 
  204. PROGRAM.  MORE ABOUT ERROR-HANDLING IN LATER LESSONS.
  205. î